package org.giovanolli.mumi.view.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 * The Class FacesContextUtils.
 */
public abstract class FacesContextUtils {

	/** The Constant RESOURCE_BUNDLE_NAME. */
	private static final String RESOURCE_BUNDLE_NAME = "msg";

	/**
	 * Adds the message to context.
	 * 
	 * @param context
	 *            the context
	 * @param key
	 *            the key
	 */
	public static void addMessageToContext(FacesContext context, String key) {
		String message = context.getApplication().getResourceBundle(context, RESOURCE_BUNDLE_NAME).getString(key);
		FacesMessage msg = new FacesMessage(message);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	/**
	 * Adds the message to context.
	 * 
	 * @param key
	 *            the key
	 */
	public static void addMessageToContext(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		addMessageToContext(context, key);
	}

	/**
	 * Gets the real path.
	 * 
	 * @return the real path
	 */
	public static String getRealPath() {
		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		return servletContext.getRealPath("");
	}
}
